﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KLY.DataModels;
using KLY.ServiceCore;

namespace KLY_Webs.Controllers
{
    public class AccountController : Controller
    {
        // GET: Account
        public ActionResult Invite(string id)
        {
            ViewBag.InviteId = id;
            return View();
        }

        public ActionResult InviteSuccess()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterReuest request)
        {
            bool isOk = true;
            if (request == null || string.IsNullOrEmpty(request.Password) || string.IsNullOrEmpty(request.ConfirmPassword)
                 || request.Password.Trim() != request.ConfirmPassword.Trim() || request.Password.Length < 6)
            {
                ViewBag.ErrMsg = "密码和确认密码不一致或密码长度小于6位";
                isOk &= false;
            }
            
            if (request == null || string.IsNullOrEmpty(request.Phone) || request.Phone.Length != 11
                || string.IsNullOrWhiteSpace(request.Verify_Code) || string.IsNullOrWhiteSpace(request.Password))
            {
                ViewBag.ErrMsg = "手机号、验证码、密码不能为空并请确认手机号是否正确";
                isOk &= false;
            }

            

            if (isOk == true)
            {
                request.Password = request.Password.Trim();
                var processer = new RegisterAccount(request);
                var response = processer.Process();
                if (response != null && response.Ok && !string.IsNullOrWhiteSpace(response.Result.token))
                {
                    BizCommon.SetLoginInfo(response.Result.token,
                        string.IsNullOrWhiteSpace(response.Result.Nick_Name) ? request.Phone : response.Result.Nick_Name,
                        response.Result.Member_Type.ToString());
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    if (response != null && !string.IsNullOrWhiteSpace(response.Message))
                    {
                        ViewBag.ErrMsg = response.Message;
                    }
                    else
                        ViewBag.ErrMsg = "注册失败请稍后再试。";
                }
            }
            
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginRequest request)
        {
            bool isOk = true;
            if (request == null || string.IsNullOrEmpty(request.Password) || request.Password.Length < 6)
            {
                ViewBag.ErrMsg = "请输入密码";
                isOk &= false;
            }

            if (request == null || string.IsNullOrEmpty(request.Phone) || request.Phone.Length != 11)
            {
                ViewBag.ErrMsg = "请确认手机号是否正确";
                isOk &= false;
            }

            if(isOk)
            {
                request.Password = request.Password.Trim();
                var processer = new LoginAccount(request);
                var response = processer.Process();
                if (response != null && response.Ok)
                {
                    BizCommon.SetLoginInfo(response.Result.token, 
                        string.IsNullOrWhiteSpace(response.Result.Nick_Name) ? request.Phone : response.Result.Nick_Name,
                        response.Result.Member_Type.ToString());
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    if(response!=null && string.IsNullOrWhiteSpace(response.Message))
                    {
                        ViewBag.ErrMsg = response.Message;
                    }
                    else
                    {
                        ViewBag.ErrMsg = "登录失败请稍后再试。";
                    }
                }
            }
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        
    }
}